package com.dayu.finecomm.protocol.hd72;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayu.finecomm.protocol.codec.FixedLengthMagicDecoder;
import com.dayu.finecomm.protocol.codec.Protocol;
import com.dayu.finecomm.protocol.pojo.TPacket;
import com.dayu.finecomm.protocol.pojo.TPacket.VType;
import com.dayu.finecomm.protocol.pojo.TSensor;

import io.netty.buffer.ByteBuf;

@Protocol(XAAAADecoder.PROTO)
public class XAAAADecoder extends FixedLengthMagicDecoder<TPacket> {

	static final String PROTO = "HD72";

	public XAAAADecoder() {

		super(new byte[] { (byte) 0xaa, (byte) 0xaa }, new byte[] { (byte) 0x0d, (byte) 0x0a }, 0, 1);
	}

	static final int P = 1;
	static final int K = 2;
	static final int A = 3;
	static final int B = 4;

	static Logger LOGGER = LoggerFactory.getLogger(XAAAADecoder.class);

	@Override
	public TPacket unpack(ByteBuf in) {

		in.skipBytes(2); // AA AA
		int length = in.readUnsignedByte(); // length
		int sn = in.readUnsignedShort();

		int nYear = 2000 + com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte());
		int nMonth = com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte()) - 1;
		int nDay = com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte());
		int nHour = com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte());
		int nMinute = com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte());
		int nSecond = 0;

		Calendar c = Calendar.getInstance();
		c.set(nYear, nMonth, nDay, nHour, nMinute, nSecond);
		c.set(Calendar.MILLISECOND, 0);

		long sTime = c.getTime().getTime();

		TPacket s = new TPacket(VType.PPP, PROTO, sn, sTime);

		int n = in.readUnsignedByte();

		int nInterval;

		short x = in.readUnsignedByte();

		nInterval = HD72Convention.interval(x);
		if (nInterval == 0)
			return null;

		double v = in.readUnsignedShort();
		v /= 100;
		s.sensors.add(new TSensor("V", v, sTime));

		int h = in.readUnsignedByte();
		s.sensors.add(new TSensor("H", h, sTime));

		for (; in.readerIndex() <= length + 2;) {

			int __ = com.dayu.finecomm.utils.Helpers.BCD(in.readUnsignedByte());

			int type = __ / 10;
			int no = __ % 10;

			// 非P、K、A、B通道
			if (type > 4 && type < 1)
				return null;

			String channel;

			switch (type) {
			case P:
				int val = (int) com.dayu.finecomm.utils.Helpers.bcdBytes2Long(in, 4);
				s.sensors.add(new TSensor("L" + no, val, sTime));
				channel = "P" + no;
				break;
			case K:
				channel = "K" + no;
				break;
			case A:
				channel = "A" + no;
				break;
			case B:
				channel = "B" + no;
				break;
			default:
				return null;
			}

			switch (type) {
			case P: // P通道
			{

				for (int i = 0, val = (int) com.dayu.finecomm.utils.Helpers.bcdBytes2Long(in, 4); i < n; i++) {

					if (i > 0) {
						val += in.readShort();
					}

					s.sensors.add(new TSensor(channel, val, sTime + nInterval * i));
				}
			}
				break;
			case K: // K通道
			{
				int val = in.readUnsignedByte();
				val = val & 0xf;
				s.sensors.add(new TSensor(channel, val, sTime));
			}
				break;
			case A:// A通道
			{
				for (int i = 0; i < n; i++) {
					int val = in.readShort();
					s.sensors.add(new TSensor(channel, val, sTime + nInterval * i));
				}
			}
				break;
			case B:// B通道
			{

				int val = in.readUnsignedByte();
				val = val & 0xf;
				s.sensors.add(new TSensor(channel, val, sTime));
			}
				break;

			default:
				return null;
			}
		}

		return s;
	}

	@Override
	public Object timing() {
		return HD72Convention.timing();
	}

	@Override
	public byte[] echo() {
		return "@222".getBytes();
	}
}
